﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;

namespace Common.Util
{
    public class FNumber
    {
        #region Format number
        public static string FormatNumber(double number, int numDecimal, string lang)
        {
            var cul = new CultureInfo(lang);
            string format = string.Format("N{0}", numDecimal.ToString(CultureInfo.InvariantCulture));
            cul.NumberFormat.CurrencyDecimalDigits = numDecimal;
            string ch = cul.NumberFormat.CurrencyDecimalSeparator;
            if (numDecimal == 0)
                return number.ToString(format);
            return number.ToString(format).TrimEnd('0').TrimEnd(ch[0]);
        }

        public static string FormatNumber(double number, int numDecimal)
        {
            string format = string.Format("N{0}", numDecimal.ToString(CultureInfo.InvariantCulture));
            string tmp = Math.Round(number, numDecimal).ToString(format);
            if (CultureInfo.CurrentCulture.Name == "vi-VN")
            {
                tmp = tmp.Replace(',', '#');
                tmp = tmp.Replace('.', ',');
                tmp = tmp.Replace('#', '.');
            }

            return tmp;
        }

        public static string FormatNumber(decimal number, int numDecimal)
        {
            return FormatNumber((double)number, numDecimal);
        }

        public static string FormatNumber(object number, int numberDecimal, string lang)
        {
            return FormatNumber(Convert.ToDouble(number), numberDecimal, lang);
        }

        public static string FormatNumber(object number, int numberDecimal)
        {
            return FormatNumber(Convert.ToDouble(number), numberDecimal);
        }

        public static string FormatNumber(object number)
        {
            return FormatNumber(number, 0);
        }

        public static string FormatNumber(double number)
        {
            return FormatNumber(number, 0);
        }

        public static string FormatNumber(decimal number)
        {
            return FormatNumber(number, 0);
        }

        public static string FormatNumber(int number)
        {
            return FormatNumber((double)number, 0);
        }

        public static decimal ConvertDecimal(string value)
        {
            return decimal.Parse(value, new CultureInfo("en-US"));
        }
        #endregion

        #region Convert number
        public static int ConvertInt(string value)
        {
            int f;
            int.TryParse(value, NumberStyles.Integer, new CultureInfo("en-US"), out f);
            return f;
        }

        public static bool IsDecimal(string value)
        {
            decimal val;
            return decimal.TryParse(value, NumberStyles.Float, new CultureInfo("en-US"), out val);
        }

        public static decimal Round2(decimal val)
        {
            long r = Convert.ToInt64(val);
            long tmp = r % 100;

            if (tmp < 50)
                return (r / 100) * 100;
            return ((r / 100) * 100) + 100;
        }

        public static decimal Round2(string val)
        {
            return Round2(decimal.Parse(val));
        }

        public static string ConvertToMoney(object sNumber)
        {
            return string.Format(new CultureInfo("en-US"), "{0:0,0}", sNumber);
        }

        /// <summary>
        /// Parse string ra double
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static double ConvertToDouble(string input)
        {
            double output;
            double.TryParse(input, out output);
            return output;
        }
        #endregion

        #region Validator
        /// <summary>
        /// Kiem tra neu chuoi nhap vao la so
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNumber(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                var isNumber = new Regex("[^0-9]");
                return !isNumber.IsMatch(str);
            }
            return false;
        }

        /// <summary>
        /// Kiem tra doi tuong co phai la Integer khong
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsInterger(object obj)
        {
            int intNumber;
            return Int32.TryParse(obj.ToString(), out intNumber);
        }

        /// <summary>
        /// Kiem tra doi tuong co phai la so thap phan khong
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsFloatNumber(string str)
        {
            return Regex.IsMatch(str, "^[-+]?[0-9]*\\.?[0-9]+$");
        }
        #endregion

        /// <summary>
        /// Return Unique long Number
        /// </summary>
        /// <returns></returns>
        public static long GetUniqueNumber()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }
    }
}
